package com.gesee.zyspps.activity;

import java.util.List;

import com.gesee.zys.R;
import com.gesee.zyspps.ZSApplication;
import com.gesee.zyspps.adapter.GridViewAdapter;
import com.gesee.zyspps.adapter.ZiduanAdapter;
import com.gesee.zyspps.db.CmsPipelineField;
import com.gesee.zyspps.db.CmsPointsName;
import com.gesee.zyspps.db.CmsPipelineFieldDao.Properties;
import com.gesee.zyspps.utils.IntentUtil;
import com.gesee.zyspps.utils.ToastUtil;
import com.gesee.zyspps.widgets.MyListView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

public class ZiduanActivity extends BaseActivity
{
    
    private MyListView listview;
    private GridView gridView;
    private GridViewAdapter gridviewAdapter;
    private int location = 0;
    private CmsPipelineField selectPoint;
    private ZiduanAdapter adapter;
    private ImageView come_back;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ziduan);
        initView();
        initClick();
    }

    private void initClick()
    {
        // TODO Auto-generated method stub
        gridView.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> adapter1, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                Log.e("position", position+"");
                if(position == 0){
                    IntentUtil.startActivity(AddZiduanActivity.class);
                }
                if(position == 1){
                    if(selectPoint !=null){
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("CmsPipelineField", selectPoint);
                        IntentUtil.startActivity(UpdateZiduanActivity.class,new Intent().putExtras(bundle));
                    }else{
                        ToastUtil.show(ZiduanActivity.this, "请先选中要修改的数据行！");
                    }
                    
                }
                if(position == 2){
                    if(selectPoint !=null){
                        ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().delete(selectPoint);
                        adapter.cleanData();
                        List<CmsPipelineField> list = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(Properties.Sort).list();
                        adapter.addData(list);
                    }else{
                        ToastUtil.show(ZiduanActivity.this, "请先选中要删除的数据行！");
                    }
                }
                if(position == 3){
                    if(selectPoint !=null){
                        List<CmsPipelineField> oldList = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(Properties.Sort).list();
                        if(selectPoint.getId() == oldList.get(0).getId()){
                            ToastUtil.show(ZiduanActivity.this, "已经排在最前面了！");
                        }else{
                            CmsPipelineField newCms = adapter.getItem(location - 1);
                            int selectPointSort = selectPoint.getSort();
                            selectPoint.setSort(newCms.getSort());
                            newCms.setSort(selectPointSort);
                            ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().update(selectPoint);
                            ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().update(newCms);
                            adapter.cleanData();
                            List<CmsPipelineField> list = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(Properties.Sort).list();
                            adapter.addData(list);
                        }
                        
                    }else{
                        ToastUtil.show(ZiduanActivity.this, "请先选中要上移的数据行！");
                    }
                }
                
                if(position == 4){
                    if(selectPoint !=null){
                        List<CmsPipelineField> oldList = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderDesc(Properties.Sort).list();
                        if(selectPoint.getId() == oldList.get(0).getId()){
                            ToastUtil.show(ZiduanActivity.this, "已经排在最后面了！");
                        }else{
                            CmsPipelineField newCms = adapter.getItem(location + 1);
                            int selectPointSort = selectPoint.getSort();
                            selectPoint.setSort(newCms.getSort());
                            newCms.setSort(selectPointSort);
                            ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().update(selectPoint);
                            ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().update(newCms);
                            adapter.cleanData();
                            List<CmsPipelineField> list = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(Properties.Sort).list();
                            adapter.addData(list);
                        }
                        
                    }else{
                        ToastUtil.show(ZiduanActivity.this, "请先选中要下移的数据行！");
                    }
                }
                if(position == 5){
                    ZiduanActivity.this.finish();
                }
            }
        });
    }
    
    @Override
    protected void onRestart()
    {
        // TODO Auto-generated method stub
        super.onRestart();
        adapter.cleanData();
        List<CmsPipelineField> list = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(Properties.Sort).list();
        adapter.addData(list);
    }

    private void initView()
    {
        // TODO Auto-generated method stub
        come_back = (ImageView) findViewById(R.id.come_back);
        come_back.setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                ZiduanActivity.this.finish();
            }
        });
        
        listview = (MyListView) findViewById(R.id.listview);
        adapter = new ZiduanAdapter(this);
        listview.setAdapter(adapter);
        List<CmsPipelineField> list = ZSApplication.getDaoSession("zyspps-db").getCmsPipelineFieldDao().queryBuilder().orderAsc(Properties.Sort).list();
        adapter.addData(list);
        
        listview.setOnItemClickListener(new OnItemClickListener()
        {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                // TODO Auto-generated method stub
                //Log.e("position", position+"");
                selectPoint = adapter.getItem(position);
                location = position;
                adapter.setSelectedPosition(position);
                adapter.notifyDataSetInvalidated();
            }
        });
        
        gridView = (GridView) findViewById(R.id.caozuo);
        gridviewAdapter = new GridViewAdapter(this);
        gridView.setAdapter(gridviewAdapter);
    }
}
